<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('vpage_template_form.class.php');
require_once('vpage_template_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_template_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');


class VPage_Template_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $hidden_path;
	private $htdocs_path;

	function __construct($system_name, Vars_Manager $vm, DB_Connection $dbc, $object_id_capture = 'obj_id', $hidden_path, $htdocs_path) {
		$this->hidden_path = tangra_normalize_path($hidden_path);
		$this->htdocs_path = tangra_normalize_path($htdocs_path);
		parent::__construct($system_name, $vm, $dbc, $object_id_capture);

		$this->export('hidden_path', $this->hidden_path.'inc/');
		$this->export('htdocs_path', $this->htdocs_path);
	}


	protected function create_form() {
		$form = new VPage_Template_Form();

//		$form->generate_html();

		return $form;
	}


	protected function create_form_view() {
		$ret = new VPage_Template_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		if (!file_exists($this->hidden_path.'inc/'.$this->form->get_field_value('wp_path'))) {
			$this->form->set_field_error('wp_path', 'file_not_exist');
			$this->export('wp_path', $this->form->get_field_value('wp_path'));
			$ret = false;
		}

		if (trim($this->form->get_field_value('edit_path'))) {
			if (!file_exists($this->htdocs_path.$this->form->get_field_value('edit_path'))) {
				$this->form->set_field_error('edit_path', 'file_not_exist_ht');
				$this->export('edit_path', $this->form->get_field_value('edit_path'));
				$ret = false;
			}
		}


		if (trim($this->form->get_field_value('man_path'))) {
			if (!file_exists($this->htdocs_path.$this->form->get_field_value('man_path'))) {
				$this->form->set_field_error('man_path', 'file_not_exist_ht');
				$this->export('man_path', $this->form->get_field_value('man_path'));
				$ret = false;
			}
		}

		if (trim($this->form->get_field_value('del_path'))) {
			if (!file_exists($this->htdocs_path.$this->form->get_field_value('del_path'))) {
				$this->form->set_field_error('del_path', 'file_not_exist_ht');
				$this->export('del_path', $this->form->get_field_value('del_path'));
				$ret = false;
			}
		}


		return $ret;
	}


	protected function on_bad_submit() {

//		printbr('lajna');
	}


	protected function create_object() {
		$object = new TCMS_VPage_Template_DBC();

		return $object;
	}


	protected function on_trans_ending(DB_Connection $dbc) {
		$form = $this->get_form();
		$object = $this->get_object();

		$update = $form->get_field_value('update');
		$cache = $form->get_field_value('cache');
		$cache_expiry = $form->get_field_value('cache_expiry');

		if ($update) {
			TCMS_VPage_DBC::update_cache_settings_for_template($dbc, $object->get_id(), $cache, $cache_expiry);
		}
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('name', $object->get_name());
		$form->set_field_value('wp_path', $object->get_wp_path());
		$form->set_field_value('wp_class_name', $object->get_wp_class_name());
		$form->set_field_value('page_name', $object->get_page_name());
		$form->set_field_value('cache', $object->get_cache());
		$form->set_field_value('cache_expiry', $object->get_cache_expiry());
		$form->set_field_value('hard_cache', $object->get_hard_cache());
		$form->set_field_value('edit_path', $object->get_edit_path());
		$form->set_field_value('man_path', $object->get_man_path());
		$form->set_field_value('del_path', $object->get_del_path());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_name($form->get_field_value('name'));
		$object->set_wp_path($form->get_field_value('wp_path'));
		$object->set_wp_class_name($form->get_field_value('wp_class_name'));
		$object->set_page_name($form->get_field_value('page_name'));
		$object->set_cache($form->get_field_value('cache'));
		$object->set_cache_expiry($form->get_field_value('cache_expiry'));
		$object->set_hard_cache($form->get_field_value('hard_cache'));
		$object->set_edit_path(trim($form->get_field_value('edit_path')));
		$object->set_man_path(trim($form->get_field_value('man_path')));
		$object->set_del_path(trim($form->get_field_value('del_path')));
	}
}